package com.xiaoma.future;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;

public class FutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("future start at "+dateFormat.format(new Date()));

        ExecutorService executorService = Executors.newSingleThreadExecutor();

        Future<String> future = executorService.submit(() -> {
            Thread.sleep(3000);
            return "future finish";
        });

        System.out.println(future.get()+" at "+dateFormat.format(new Date()));




        System.out.println("completableFuture start at "+ dateFormat.format(new Date()));
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "completablefuture finish";
        });

        System.out.println(completableFuture.get()+" at "+dateFormat.format(new Date()));
        System.out.println("主线程会被阻塞，时间"+dateFormat.format(new Date()));
    }
}
